'use strict';
const Service = require('egg').Service;

//
class MiningService extends Service {
  // 添加一个token池子
  async addToken2(tokenAddress, name) {
    const { app, ctx, service } = this;
    const contract  = app.BSC.NftMiningContract;
    return await contract.addToken(tokenAddress, name).send({
      feeLimit: 100000000,
      callValue: 0,
      // true就是20块确认才会给返回值, false就是1个确认就给返回值;
      shouldPollResponse: false
    });
  }

  // 添加一个lp池子
  async addLp2(tokenAddress, name) {
    const { app, ctx, service } = this;
    const contract  = app.BSC.NftMiningContract;
    return await contract.addLp(tokenAddress, name).send({
      feeLimit: 100000000,
      callValue: 0,
      // true就是20块确认才会给返回值, false就是1个确认就给返回值;
      shouldPollResponse: false
    });
  }

  // 查询所有的池子
  async getPools2() {
    const { app, ctx, service } = this;
    const contract  = app.BSC.NftMiningContract;
    const res = await contract.methods.getPools().call();
    const arr = [];
    for (let i = 0; i < res._r.length; i++) {
      arr.push(await service.eth.addressFromHex(res._r[i]))
    }
    return arr;
  }

  // 关闭或开启某个token池子
  async openOrClosePool2(poolAddress) {
    const { app, ctx, service } = this;
    const contract  = app.BSC.NftMiningContract;
    return await contract.openOrClosePool(poolAddress).send({
      feeLimit: 100000000,
      callValue: 0,
      // true就是20块确认才会给返回值, false就是1个确认就给返回值;
      shouldPollResponse: false
    });
  }

  // 查询池子的详情详细
  async poolMapping2(poolAddress) {
    const { app, ctx, service } = this;
    const contract  = app.BSC.NftMiningContract;
    //console.log(contract);
    const res = await contract.methods.poolMapping(poolAddress).call();
    console.log(res);
    return {
      typeOf: ctx.helper.hexToDec(res.typeOf),
      isOpen: res.isOpen,
      name: res.name,
      totalDeposit: ctx.helper.hexToDec(res.totalDeposit._hex),
      totalWithdraw: ctx.helper.hexToDec(res.totalWithdraw._hex),
      totalRed: ctx.helper.hexToDec(res.totalRed._hex),
      playerNumber: ctx.helper.hexToDec(res.playerNumber._hex)
    };
  }

  // 查询用户的某个池子质押信息
  async getUserPoolMsg2(poolAddress, userAddress) {
    const { app, ctx, service } = this;
    const contract  = app.BSC.NftMiningContract;
    const res = await contract.methods.poolUserMapping(poolAddress, userAddress).call();
    console.log('res:', res);
    return {
      amount: ctx.helper.hexToDec(res.amount._hex),
      red: ctx.helper.hexToDec(res.red._hex),
      player: res.player
    };
  }

  // 查询用户地址是否是有效用户
  async isUser2(userAddress) {
    const { app, ctx, service } = this;
    const contract  = app.BSC.NftMiningContract;
    // 返回true或false
    return await contract.methods.isUserMapping(userAddress).call();
  }

  // 查询双重签名是否使用
  async signHashStatus2(messageHash) {
    const { app, ctx, service } = this;
    const contract  = app.BSC.NftMiningContract;
    // 返回true或false
    return await contract.methods.signHash(messageHash).call();
  }

  // 用户提取质押收益的二次签名, Mining
  async secondSignature2(
    poolAddress, userAddress, superAddress, superSuperAddress, value, superValue, superSuperValue, nonce
  ) {
    const { app, ctx, service } = this;
    const privateKey = app.BSC.OwnerPrivateKey;
    const sha3Data = app.ETH.Web3.utils.soliditySha3(
      "userRed", poolAddress, userAddress, superAddress, superSuperAddress, value, superValue, superSuperValue, nonce
    );
    return app.ETH.Web3.eth.accounts.sign(sha3Data, privateKey);
  }

  // 挖矿合约监听的封装
  async scanEvent(event_name, min_timestamp, max_timestamp) {
    const { app, ctx, service } = this;
    const tronGrid = app.BSC.TronGrid;
    const contractAddress = app.BSC.NftMiningAddress;
    // 监听这个事件的时间区间之内的所有事件,但是不能超过200条
    const options1 = {
      event_name: event_name,
      only_confirmed: false,
      // block_number: '18124224',
      min_timestamp: min_timestamp,
      max_timestamp: max_timestamp,
      limit: 200,
    };
    const eventRes1 = await tronGrid.contract.getEvents(contractAddress, options1);
    return eventRes1.data;
  }



}

module.exports = MiningService;
